const webpack = require('webpack');
const path = require('path');
const imageminMozjpeg = require('imagemin-mozjpeg');
const imageminPngquant = require('imagemin-pngquant');


module.exports = {
  entry: './index.js',
  output: {
    filename: 'bundle.js',
    path: path.join(__dirname, 'dist'),
    publicPath: '/'
  },
  context: path.join(__dirname, 'src'),

  module: {
    rules: [
      {
        test: /\.js$/,
        use: [
          'babel-loader',
        ],
        exclude: /node_modules/
      },
      {
        test: /\.css$/,
        use: [
          { loader: "style-loader" },
          { loader: "css-loader" },
        ],
      },
      {
        test: /\.less$/,
        use: [
          'style-loader',
          { loader: 'css-loader', options: { importLoaders: 1 } },
          'less-loader'
        ]
      },
      {
        test: /\.(jpe?g|png|gif|svg)$/i,   //url-load引入图片\image-webpack-loader压缩图片
        loaders: [
          'url-loader?limit=8192&name=[path][hash].[ext]', {
           loader: 'image-webpack-loader',
          }
        ],
      },
    ],
  },

  plugins: [

    //jquerys
    new webpack.ProvidePlugin({
      $:"jquery",
      jQuery:"jquery",
      "window.jQuery":"jquery"
    }),

    //切换生产环境
    new webpack.DefinePlugin({
      'process.env': {
        NODE_ENV: JSON.stringify('production')
      }
    }),

    //压缩图片
    imageminMozjpeg({
      quality: 100,
      progressive: true
    }),
    imageminPngquant({
      quality: '65-90'
    }),


    // 压缩时去掉js所有注释，包括copyright信息
    new webpack.optimize.UglifyJsPlugin({
      output: {
        comments: false,  // remove all comments
      },
      compress: {
        warnings: false
      },
      except: ['$super', '$', 'exports', 'require']    //排除关键字
    })
  ],
};